package com.oasistudio.ads.expert;

import java.util.HashSet;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.mobclick.android.MobclickAgent;
import com.oasistudio.ads.expert.banner.AdViewAdwo;
import com.oasistudio.ads.expert.banner.AdViewAiwan;
import com.oasistudio.ads.expert.banner.AdViewAppMedia;
import com.oasistudio.ads.expert.banner.AdViewCasee;
import com.oasistudio.ads.expert.banner.AdViewDomob;
import com.oasistudio.ads.expert.banner.AdViewGoogle;
import com.oasistudio.ads.expert.banner.AdViewSmartmad;
import com.oasistudio.ads.expert.banner.AdViewTaobao;
import com.oasistudio.ads.expert.banner.AdViewVpon;
import com.oasistudio.ads.expert.banner.AdViewWiyun;
import com.oasistudio.ads.expert.banner.AdViewYoumi;

/** 各个联盟的横幅广告荟萃。 */
public class BannerGallery extends Activity {
	private EditText pidTextView;
	AdsExpertApplication expertApp;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobclickAgent.onError(this);// 友盟错误统计
		MobclickAgent.update(this);// 友盟自动更新
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		expertApp = (AdsExpertApplication) getApplicationContext();

		allBannerAdView();

		// 添加一个事件监听，连续点击5次，弹出填写pid的界面
		this.pidTextView = new EditText(this);
		// this.pidTextView.setLayoutParams(new
		// LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		LinearLayout adContainer = (LinearLayout) this.findViewById(R.id.ads_container);
		adContainer.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// System.out.println("@@@@@@BannerGallery.adContainer.onClick()...........");
				// TODO Auto-generated method stub
				click(System.currentTimeMillis());
			}
		});

	}

	private Set<String> loadCheckedUnion() {
		SharedPreferences settings = getSharedPreferences(SelectUnion.class.getName(), MODE_PRIVATE);
		String unionsSelected = settings.getString("unionsSelected", null);
		System.out.println("-----------------------BannerGallery.loadCheckedUnion() load unionsSelected = " + unionsSelected);
		Set<String> set = new HashSet<String>();
		if (unionsSelected != null) {
			for (String union : unionsSelected.split(",")) {
				set.add(union.trim());
			}
		}
		System.out.println("-----------------------BannerGallery.loadCheckedUnion() load set = " + set);
		return set;
	}

	private void allBannerAdView() {
		LinearLayout adContainer = (LinearLayout) findViewById(R.id.ads_container);
		adContainer.removeAllViews();
		
		// 根据选择的联盟名称，显示相应联盟的横幅广告
		Set<String> unionsChecked = this.loadCheckedUnion();
		System.out.println("-----------------------BannerGallery.allBannerAdView() load unionsChecked = " + unionsChecked);
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewWiyun.UNION)) {
			// http://www.wiyun.com
			new AdViewWiyun(this);
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewTaobao.UNION)) {
			// http://munion.taobao.com
			new AdViewTaobao(this, loadPID());
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewYoumi.UNION)) {
			// http://www.youmi.net
			new AdViewYoumi(this);
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewAppMedia.UNION)) {
			// http://www.appmedia.cn
			new AdViewAppMedia(this);
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewDomob.UNION)) {
			// http://www.domob.cn
			new AdViewDomob(this);
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewCasee.UNION)) {
			// http://www.casee.cn
			new AdViewCasee(this);
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewVpon.UNION)) {
			// http://cn.adon.vpon.com
			new AdViewVpon(this);
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewSmartmad.UNION)) {
			// http://www.smartmad.com
			new AdViewSmartmad(this);
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewGoogle.UNION)) {
			// http://google.com
			this.googleAdView = new AdViewGoogle(this).getAdView();
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewAdwo.UNION)) {
			// http://www.adwo.com
			new AdViewAdwo(this);
		}
		if (unionsChecked.size() == 0 || unionsChecked.contains(AdViewAiwan.UNION)) {
			// http://www.iaiwan.com
			new AdViewAiwan(this);
		}

	}

	private com.taobao.munion.ads.clientSDK.TaoAds taoAds;

	private void setTaobaoPID() {
		if (pidTextView.getText() != null && pidTextView.getText().toString().trim().length() > 0) {
			if (taoAds != null) {
				taoAds.setPid(pidTextView.getText().toString().trim());
			}
		}
		// 将设置的PID暂存，下次弹出对话框时显示
		savePID();
	}

	private void savePID() {
		SharedPreferences settings = this.getSharedPreferences(BannerGallery.class.getName(), MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		String pid = this.pidTextView.getText().toString();
		System.out.println(",,,,,,,,,,,,,,,,BannerGallery.savePID() pid = " + pid);
		editor.putString("pid", pid);
		editor.commit();
	}

	private String loadPID() {
		SharedPreferences settings = getSharedPreferences(BannerGallery.class.getName(), MODE_PRIVATE);
		String pid = settings.getString("pid", null);
		System.out.println(",,,,,,,,,,,,,,,,BannerGallery.loadPID() pid = " + pid);
		if (this.pidTextView != null)
			this.pidTextView.setText(pid);
		return pid;
	}

	private long lastClickTime;
	private int clickCount;

	private void click(long time) {
		if ((time - lastClickTime) < 500) {
			clickCount = 1;
			lastClickTime = time;
		} else {
			clickCount++;
		}
		if (clickCount == 5) {
			clickCount = 0;
			this.loadPID();
			// 弹出填写pid的框框
			new AlertDialog.Builder(BannerGallery.this).setTitle("请输入淘宝PID：")
					.setIcon(android.R.drawable.ic_dialog_info).setView(pidTextView)
					// .setView(new EditText(BannerGallery.this))
					.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							setTaobaoPID();
							// TODO Auto-generated method stub
							ViewParent viewParent = pidTextView.getParent();
							if (viewParent instanceof ViewGroup) {
								((ViewGroup) viewParent).removeView(pidTextView);
							}
						}

					}).setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							ViewParent viewParent = pidTextView.getParent();
							if (viewParent instanceof ViewGroup) {
								((ViewGroup) viewParent).removeView(pidTextView);
							}
						}

					}).show();
		}
	}

	private com.google.ads.AdView googleAdView;

	public void onDestroy() {
		if (googleAdView != null)
			googleAdView.destroy();
		super.onDestroy();
	}

	/************************** umeng.com start *************************/
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		allBannerAdView();
		System.out.println("---------------------------BannerGallery.onResume().");
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	/************************** umeng.com end *************************/
}